package library;

import java.util.Scanner;

public class LibraryManagementSystem {
	private static Library library;
    private static Administrator administrator;
    private static Student student;
    private static Scanner scanner;

    public static void main(String[] args) {
        library = new Library();
        administrator = new Administrator("admin123", "Admin", "Male", "admin123");
        student = new Student("student123", "John Doe", "Male", "student123");
        scanner = new Scanner(System.in);

        showMainMenu();
    }

    private static void showMainMenu() {
        System.out.println("========== 图书馆管理系统 ==========");
        System.out.println("1. 登录");
        System.out.println("0. 退出");
        System.out.println("==================================");
        System.out.print("请输入选项：");

        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                login();
                break;
            case 0:
                System.out.println("感谢使用图书馆管理系统，再见！");
                break;
            default:
                System.out.println("无效的选项，请重新输入。\n");
                showMainMenu();
        }
    }

    private static void login() {
        System.out.println("\n========== 登录 ==========");
        System.out.print("请输入用户类型（1.管理员 2.普通用户 ）：");
        int userType = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        if (userType == 1) {
            loginAsAdministrator();
        } else if (userType == 2) {
            loginAsStudent();
        } else {
            System.out.println("无效的选项，请重新输入。\n");
            login();
        }


    }

    private static void loginAsAdministrator() {
        System.out.print("请输入管理员帐号：");
        String adminId = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();

        if (adminId.equals(administrator.getId()) && password.equals(administrator.getPassword())) {
            showAdministratorMenu();
        } else {
            System.out.println("登录失败，请检查管理员帐号和密码是否正确。\n");
            showMainMenu();
        }
    }

    private static void loginAsStudent() {
        System.out.print("请输入您的图书帐号：");
        String studentId = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();

        if (studentId.equals(student.getId()) && password.equals(student.getPassword())) {
            showStudentMenu();
        } else {
            System.out.println("登录失败，请检查图书帐号和密码是否正确。\n");
            showMainMenu();
        }
    }

    private static void showAdministratorMenu() {
        System.out.println("\n========== 管理员菜单 ==========");
        System.out.println("1. 添加书籍");
        System.out.println("2. 删除书籍");
        System.out.println("3. 修改图书库存");
        System.out.println("4. 查询书籍");
        System.out.println("0. 返回上一级菜单");
        System.out.println("==================================");
        System.out.print("请输入选项：");

        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                removeBook();
                break;
            case 3:
                updateBookQuantity();
                break;
            case 4:
                searchBooks();
                break;
            case 0:
                login();
                break;
            default:
                System.out.println("无效的选项，请重新输入。\n");
                showAdministratorMenu();
        }
    }

    private static void addBook() {
        System.out.println("\n========== 添加书籍 ==========");
        System.out.print("请输入该图书ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // Consume newline
        System.out.print("请输入该图书类别: ");
        String category = scanner.nextLine();
        System.out.print("请输入该图书书名: ");
        String title = scanner.nextLine();
        System.out.print("请输入该图书作者: ");
        String author = scanner.nextLine();
        System.out.print("请输入该图书出版社: ");
        String publisher = scanner.nextLine();
        System.out.print("请输入该图书单价: ");
        double price = scanner.nextDouble();
        System.out.print("请输入该图书现存量: ");
        int quantity = scanner.nextInt();

        Book book = new Book(id, category, title, author, publisher, price, quantity);
        library.addBook(book);

        showAdministratorMenu();
    }

    private static void removeBook() {
        System.out.println("\n========== 删除书籍 ==========");
        System.out.print("请输入要删除的图书编号: ");
        int bookId = scanner.nextInt();

        library.removeBook(bookId);

        showAdministratorMenu();
    }

    private static void updateBookQuantity() {
        System.out.println("\n========== 修改图书库存  ==========");
        System.out.print("请输入图书编号: ");
        int bookId = scanner.nextInt();
        System.out.print("请输入要修改的库存数量: ");
        int quantity = scanner.nextInt();

        library.updateBookQuantity(bookId, quantity);

        showAdministratorMenu();
    }

    private static void searchBooks() {
        System.out.println("\n==========查询书籍 ==========");
        System.out.println("1. 以书名查询");
        System.out.println("2. 以书号查询");
        System.out.println("0. 返回上一级菜单");
        System.out.println("==================================");
        System.out.print("请输入选项：");

        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                searchBooksByTitle();
                break;
            case 2:
                searchBooksById();
                break;
            case 0:
                showStudentMenu();
                break;
            default:
                System.out.println("无效的选项，请重新输入。\n");
                searchBooks();
        }
    }

    private static void searchBooksByTitle() {
        System.out.print("请输入书名: ");
        String title = scanner.nextLine();

        library.searchBooksByTitle(title);

        searchBooks();
    }

    private static void searchBooksById() {
        System.out.print("请输入图书编号: ");
        int bookId = scanner.nextInt();

        library.searchBooksById(bookId);

        searchBooks();
    }

    private static void showStudentMenu() {
        System.out.println("\n========== 普通用户菜单 =============");
        System.out.println("1. 查询图书");
        System.out.println("2. 借书");
        System.out.println("3. 还书");
        System.out.println("0. 返回上一级菜单");
        System.out.println("==================================");
        System.out.print("请输入选项：");

        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                searchBooks();
                break;
            case 2:
                borrowBook();
                break;
            case 3:
                returnBook();
                break;
            
            case 0:
                showMainMenu();
                break;
            default:
                System.out.println("无效的选项，请重新输入。\n");
                showStudentMenu();
        }
    }

    
               

	private static void borrowBook() {
        System.out.print("请输入要借阅的图书编号: ");
        int bookId = scanner.nextInt();

        Book book = getBookById(bookId);
        if (book != null) {
            student.borrowBook(book);
        }

        showStudentMenu();
    }

    private static void returnBook() {
        System.out.print("请输入要归还图书编号: ");
        int bookId = scanner.nextInt();

        Book book = getBookById(bookId);
        if (book != null) {
            student.returnBook(bookId);
        }

        showStudentMenu();
    }

    private static Book getBookById(int bookId) {
        for (Book book : library.getBooks()) {
            if (book.getId() == bookId) {
                return book;
            }
        }
        System.out.println("该书未入图书馆！");
        return null;
        }
        
        }

    


